<template>
  <div class="login-log">
    <!-- 搜索表单 -->
    <div class="search-form">
      <el-form :model="searchForm" :inline="true" @submit.prevent="handleSearch">
        <el-form-item label="用户名">
          <el-input
            v-model="searchForm.userName"
            placeholder="请输入用户名"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="登录状态">
          <el-select
            v-model="searchForm.status"
            placeholder="请选择状态"
            clearable
            style="width: 120px"
          >
            <el-option label="成功" value="0" />
            <el-option label="失败" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="登录时间">
          <el-date-picker
            v-model="searchForm.dateRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">搜索</el-button>
          <el-button :icon="Refresh" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 数据表格 -->
    <div class="table-container">
      <div class="table-header">
        <div class="table-title">登录日志</div>
        <div class="table-actions">
          <el-button type="success" :icon="Download" @click="handleExport">导出</el-button>
          <el-button type="danger" :icon="Delete" @click="handleClean">清空</el-button>
        </div>
      </div>

      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column prop="logId" label="日志ID" width="80" />
        <el-table-column prop="userName" label="用户名" width="120" />
        <el-table-column prop="ipAddress" label="IP地址" width="120" />
        <el-table-column prop="location" label="登录地点" width="150" />
        <el-table-column prop="browser" label="浏览器" width="120" />
        <el-table-column prop="os" label="操作系统" width="120" />
        <el-table-column prop="status" label="状态" width="80">
          <template #default="{ row }">
            <el-tag :type="row.status === '0' ? 'success' : 'danger'">
              {{ row.status === '0' ? '成功' : '失败' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="message" label="提示信息" width="200" />
        <el-table-column prop="loginTime" label="登录时间" width="160" />
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.pageNum"
        v-model:page-size="pagination.pageSize"
        :total="pagination.total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Refresh, Download, Delete } from '@element-plus/icons-vue'
import type { LoginLog } from '@/types/monitor'

// 搜索表单
const searchForm = reactive({
  userName: '',
  status: '',
  dateRange: []
})

// 表格数据
const tableData = ref<LoginLog[]>([])
const loading = ref(false)

// 分页
const pagination = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0
})

// 获取登录日志列表
const getLoginLogList = async () => {
  try {
    loading.value = true
    // 模拟数据
    tableData.value = [
      {
        logId: 1,
        userName: 'admin',
        ipAddress: '192.168.1.100',
        location: '北京市',
        browser: 'Chrome 120.0',
        os: 'Windows 10',
        status: '0',
        message: '登录成功',
        loginTime: '2024-01-27 10:30:00'
      },
      {
        logId: 2,
        userName: 'user001',
        ipAddress: '192.168.1.101',
        location: '上海市',
        browser: 'Firefox 121.0',
        os: 'macOS 14.0',
        status: '0',
        message: '登录成功',
        loginTime: '2024-01-27 09:15:00'
      },
      {
        logId: 3,
        userName: 'user002',
        ipAddress: '192.168.1.102',
        location: '广州市',
        browser: 'Safari 17.0',
        os: 'macOS 14.0',
        status: '1',
        message: '密码错误',
        loginTime: '2024-01-27 08:45:00'
      },
      {
        logId: 4,
        userName: 'user003',
        ipAddress: '192.168.1.103',
        location: '深圳市',
        browser: 'Edge 120.0',
        os: 'Windows 11',
        status: '0',
        message: '登录成功',
        loginTime: '2024-01-27 08:20:00'
      }
    ]
    pagination.total = 4
  } catch (error) {
    ElMessage.error('获取登录日志失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.pageNum = 1
  getLoginLogList()
}

// 重置
const handleReset = () => {
  Object.assign(searchForm, {
    userName: '',
    status: '',
    dateRange: []
  })
  handleSearch()
}

// 导出
const handleExport = () => {
  ElMessage.info('导出功能开发中...')
}

// 清空
const handleClean = async () => {
  try {
    await ElMessageBox.confirm('确定要清空所有登录日志吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    ElMessage.success('清空成功')
    getLoginLogList()
  } catch (error) {
    // 用户取消
  }
}

// 分页大小变化
const handleSizeChange = (size: number) => {
  pagination.pageSize = size
  getLoginLogList()
}

// 当前页变化
const handleCurrentChange = (page: number) => {
  pagination.pageNum = page
  getLoginLogList()
}

onMounted(() => {
  getLoginLogList()
})
</script>

<style lang="scss" scoped>
.login-log {
  .search-form {
    background: #fff;
    padding: 20px;
    border-radius: 8px;
    margin-bottom: 20px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }

  .table-container {
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    overflow: hidden;
  }

  .table-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid #ebeef5;
  }

  .table-title {
    font-size: 16px;
    font-weight: 500;
    color: #303133;
  }

  .table-actions {
    display: flex;
    gap: 8px;
  }
}
</style>
